﻿Imports DAL
Public Class UCListeVille
    Public Sub chargerVille()
        For Each ville In (From v In bd.Villes Select v)
            Dim item As New ListViewItem
            item.Tag = ville.ID
            item.Text = ville.Nom
            ListView1.Items.Add(item)
        Next
    End Sub


    Private Sub BtAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAjouter.Click
        If Not String.IsNullOrEmpty(TbVille.Text) And Not (From i As ListViewItem In ListView1.Items Select i.Text.ToLower).Contains(TbVille.Text.ToLower.Trim) Then
            Dim ville As New Ville
            Dim item As New ListViewItem
            ville.Nom = StrConv(TbVille.Text, vbProperCase)
            bd.Villes.InsertOnSubmit(ville)
            bd.SubmitChanges()
            item.Text = ville.Nom
            item.Tag = ville.ID
            ListView1.Items.Add(item)
            TbVille.Text = String.Empty
        End If
    End Sub

    Private Sub BtSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSupprimer.Click
        Dim employes = String.Empty
        Dim beneneficiaires = String.Empty
        Dim contacts = String.Empty
        Dim utilise As Boolean
        If ListView1.SelectedItems.Count > 0 AndAlso MsgBox("Voulez-vous vraiment supprimer cette ville", vbYesNo, "Avertissement") = vbYes Then
            Dim ville = (From v In bd.Villes Where v.ID = Integer.Parse(ListView1.SelectedItems(0).Tag) Select v).First
            For Each Employe In (From emp In bd.Employes Where emp.Ville = ville.ID Select emp)
                employes = employes + Employe.Prenom + " " + Employe.Nom + ", "
                utilise = True
            Next
            For Each beneficiaire In (From b In bd.Beneficiaires Where b.Ville = ville.ID Select b)
                beneneficiaires = beneneficiaires + beneficiaire.Prenom + " " + beneficiaire.Nom + ", "
                utilise = True
            Next
            For Each contact In (From c In bd.PersonneRessources Where c.Ville = ville.ID Select c)
                contacts = contacts + contact.Prenom + " " + contact.Nom + ", "
                utilise = True
            Next
            If Not utilise Then
                bd.Villes.DeleteOnSubmit(ville)
                bd.SubmitChanges()
                bd.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, bd.Employes)
                bd.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, bd.Beneficiaires)
                bd.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, bd.PersonneRessources)
                ListView1.Items.Remove(ListView1.SelectedItems(0))
            Else
                Dim message = "Cette ville est déjà utilisé "
                If Not employes = String.Empty Then
                    message = message + Chr(13) + "Employé: " + employes
                    message.Remove(message.Length - 1)
                End If
                If Not beneneficiaires = String.Empty Then
                    message = message + Chr(13) + "Bénéficiaire: " + beneneficiaires
                    message.Remove(message.Length - 1)
                End If
                If Not contacts = String.Empty Then
                    message = message + Chr(13) + "Contact: " + contacts
                    message.Remove(message.Length - 1)
                End If
                MsgBox(message)
            End If
        End If
    End Sub

    Private Sub UCListeVille_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.DesignMode Then
            chargerVille()
        End If
    End Sub
End Class
